/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/11/20 9:36
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.message.common.config;

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * 消息缓存配置
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/11/20 9:36
 */
@Configuration
public class MessageCacheConfiguration {

    public static final String VERIFICATION_CACHE = "verificationCache";

    @Qualifier("hazelcastServerInstance")
    @Autowired
    private HazelcastInstance hazelcastInstance;

    @Bean(name = VERIFICATION_CACHE)
    public Map<String, String> verificationCache() {
        Config config = hazelcastInstance.getConfig();

        MapConfig mapConfig = config.getMapConfig(VERIFICATION_CACHE)
                // 设定生存时间, 测试用120，生产应该足够大，比如3600秒
                .setTimeToLiveSeconds(300)
                // 设置最大空闲秒数
                .setMaxIdleSeconds(3600)
                .setEvictionPolicy(EvictionPolicy.LRU);
        config.addMapConfig(mapConfig);

        return hazelcastInstance.getMap(VERIFICATION_CACHE);
    }

}
